<?php
include('function/connection.php'); 
if(isset($_GET['from_profit_date']) && isset($_GET['to_profit_date']) && $_GET['from_profit_date']!='' && $_GET['to_profit_date']!='' )
{
	//get Rate of money
	$sql_rate="SELECT money FROM  tbl_currency ";
	$result_rate=mysql_query($sql_rate);
	$row_rate=mysql_fetch_assoc($result_rate);
	$rate_money=$row_rate['money'];
	
			$selected_date=$_REQUEST['from_profit_date'];
		  	$selected_date=strtotime( $selected_date );
			$mysqldate = date( 'Y-m-d', $selected_date );
$fromdate=$mysqldate. " To<br/>";

			$selected_date=$_REQUEST['to_profit_date'];
		  	$selected_date=strtotime( $selected_date );
			$mysqldate = date( 'Y-m-d', $selected_date );
$todate=$mysqldate;
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Sale Report</title>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
</head>
<style type="text/css" media="print">
.hide{display:none}
</style>
<script type="text/javascript">
function printpage() {
document.getElementById('printButton').style.visibility="hidden";
window.print();
document.getElementById('printButton').style.visibility="visible";  
}
</script>
<body style="font-size:13px;">
<input name="print" type="button" value="Print" id="printButton" onClick="printpage()">
<table width="100%" border="0" cellspacing="0" cellpadding="0">
  <tr>
    <td align="center" colspan="2">
      <table width="700" border="0" cellspacing="0" cellpadding="0">
        <tr>
          <td height="30" align="left"><h3><img src="images/logo_marly.png" width="130"  height="40" />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Sales Profit Report </h3></td>
          <td width="35%"><strong>Hay Soeung Company</strong><br />
Russian Blvd Sang Kat Toek Thla, <br />Khan Sen Sok, Phnom Penh, Cambodia<br />
Email: hay_soeng@yahoo.com<br/>
Website:&nbsp;www.haysoeungautomobile.com<br />
Tel: <span large;\>+85512 544498</span><br />&nbsp;&nbsp;&nbsp;&nbsp; &nbsp;&nbsp;+85512 287899<br/>
			
		  </td>
        </tr>
        <tr>
          <td height="30" align="center" colspan="2">&nbsp;</td>
        </tr>
        <tr>
          <td align="left" colspan="2">
          <table width="300" border="0" cellspacing="0" cellpadding="0">
            <tr>
              <td width="150"><strong>Total Sales </strong></td>
              <td width="150">&nbsp;
			  	<?php 
					$sql="SELECT sum(amount) as total_amout FROM tbl_sales WHERE count=1 AND sale_date BETWEEN '$fromdate' AND '$todate'";
					$result=mysql_query($sql);
					$row=mysql_fetch_assoc($result);
					echo $amount=$row['total_amout']."$";
					$amount=$row['total_amout'];
				?>
              </td>
            </tr>
            <tr>
              <td><strong>Received Amount</strong></td>
              <td>&nbsp;
			  	<?php 
					$sql="SELECT sum(paid) as paid FROM tbl_sales where count=1 AND sale_date BETWEEN '$fromdate' AND '$todate' ";
					$result=mysql_query($sql);
					$row=mysql_fetch_assoc($result);
					echo $row['paid']."$";
				?>
                    </td>
            </tr>
            <tr>
              <td width="150"><strong>Total OutStanding </strong></td>
              <td width="150">&nbsp;
			  <?php 
				$sql="SELECT sum(balance) as balance FROM tbl_sales where count=1 AND sale_date BETWEEN '$fromdate' AND '$todate' ";
				$result=mysql_query($sql);
				$row=mysql_fetch_assoc($result);
				echo $row['balance']."$";
				?>
              </td>
            </tr>
            <tr>
              <td width="150"><b style="color:#F00";>Total Profit</b></td>
              <td width="150">&nbsp;
			  <?php 
				$sql="SELECT sum(total_buying) as total_buying FROM v_total_buying where sale_date BETWEEN '$fromdate' AND '$todate' ";
				$result=mysql_query($sql);
				$row=mysql_fetch_assoc($result);
				$total_buying=$row['total_buying'];
				$profit=$amount-$total_buying;
				echo "<b style=\"color:#F00;\">". $profit.".00$</b>";
				?>
              </td>
            </tr>
          </table></td>
        </tr>
        <tr>
          <td width="45" colspan="2"><hr></td>
        </tr>
        <tr>
          <td height="20" colspan="2"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
              <tr>
                <td width="45"><strong>From</strong></td>
                <td width="393">&nbsp;<?php echo $_GET['from_profit_date']; ?></td>
                <td width="41"><strong>To</strong></td>
                <td width="116">&nbsp;<?php echo $_GET['to_profit_date']; ?></td>
              </tr>
          </table></td>
        </tr>
        <tr>
          <td width="45" colspan="2"><hr></td>
        </tr>
        <tr>
          <td colspan="2"><table width="100%"  border="0" cellspacing="0" cellpadding="0">
			  <tr>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
                <td>&nbsp;</td>
              </tr>
			  <?php 
			  	$sql="SELECT * FROM tbl_sales where sale_date BETWEEN '$fromdate' AND '$todate' ORDER BY sale_date DESC";
				$result=mysql_query($sql);
				$j=1;
				$sale_id="";
				while ($line = mysql_fetch_object($result)) {
				?>
                <tr>
                	<td colspan="6" align="left">
						<?php
                            if($sale_id!= $line->sale_id) {
								$total=$line->paid; 
								$number=$total*$rate_money;
                                echo "<b>Sale ID&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</b>&nbsp;&nbsp;".$line->sale_id;
								echo "&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<b>Customer&nbsp;&nbsp;:</b>&nbsp;&nbsp;".$line->cus_name."<br>"; 
								echo "<b>Sub Total&nbsp;&nbsp;:</b>&nbsp;&nbsp;".$line->amount;
								echo "&nbsp;&nbsp;&nbsp;&nbsp;<b>Balance&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</b>&nbsp;&nbsp;".$line->balance."<br>";
								echo "<b>Paid&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</b>&nbsp;&nbsp;".$line->paid;
								echo "&nbsp;&nbsp;&nbsp;&nbsp;<b>Riel&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:</b>&nbsp;&nbsp;".number_format($number, 2, '.', ',')."R<br/>";  
                                $sale_id = $line->sale_id;
								 
								$sql_total="SELECT  sum(total_buying) as total_buying,amount FROM v_total_buying WHERE sale_id='$sale_id'";
								$result_total=mysql_query($sql_total);
								$row_total=mysql_fetch_object($result_total);
								$amount= $row_total->amount;
								$buying_total= $row_total->total_buying;
								$profit=$amount-$buying_total;
								echo "<b style=\"color:#F00;\">Profit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;:&nbsp;&nbsp;".$profit.".00$</b>";
;
								echo "<br><br>";
					?> 
                        <tr>
                            <td width="5%" align="left"><strong>No</strong></td>
                            <td width="10%" align="left"><strong>Date</strong></td>
                            <td width="25%" align="left"><strong>Product </strong></td>
                            <td width="5%" align="center"><strong>Qty </strong></td>
                            <td width="10%" align="center"><strong>Rate</strong></td>
                            <td width="10%"align="center"><strong>Total</strong></td>
             		    </tr>
                       <tr> 
              				<td colspan="6"><hr></td>
              			</tr>
                        <?php
                            } 
                        ?>
                   </td>
                </tr>
				<tr> 
              </tr>
                    <td><?php echo "00".$j; ?></td>
                    <td align="left"><?php  $mysqldate=$line->sale_date;
            $phpdate = strtotime( $mysqldate );
            $phpdate = date("d/m/Y",$phpdate);
            echo $phpdate; ?></td>
                    <td ><?php echo $line->pd_name; ?></td>
                    <td align="center"><?php echo $line->qty; ?></td>
                    <td align="center"><?php echo $line->selling_price."$"; ?></td>
                    <td align="center"><?php echo $line->total."$"; ?></td> 
              <tr> 
              		<td colspan="6"><hr></td>
              </tr>
			  <?php
			  		$j++;
				}
			  ?>
          </table></td>
        </tr>
        <tr>
          <td colspan="2">&nbsp;</td>
        </tr>
        <tr>
          <td colspan="2">&nbsp;</td>
        </tr>
    </table></td>
  </tr>
</table>
</body>
</html>
<?php
}
else{
	echo "Please from and to date to process report";
}
?>